<%@ page  contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8"%>

<%@include file="../../common/common-head.jsp"%>

<body>

<!-- begin #content -->
<div id="content" class="content">
    <c:set var="listName" value="文章管理" />
    <%@include file="../../common/common-breadcrumb.jsp"%>

    <!-- begin row -->
    <div class="row">
        <!-- begin col-6 -->
        <div class="col-xl-8">
            <!-- begin panel -->
            <div class="panel panel-inverse" data-sortable-id="table-basic-1">
                <c:set var="panelName" value="文章管理" />
                <%@include file="../../common/common-panel-heading.jsp"%>

                <!-- begin panel-body -->
                <div class="panel-body">
                    <form class="form-inline" action="/" method="POST" style="margin-bottom: 10px;">
                        <div class="form-group m-r-10">
                            <input type="text" class="form-control" id="author" placeholder="文章作者" value="${author}" />
                        </div>
                        <div class="form-group m-r-10">
                            <input type="text" class="form-control" id="title" placeholder="文章标题" value="${title}" />
                        </div>
                        <button type="button" class="btn btn-sm btn-primary m-r-5" id="search">
                            <i class="fa fa-search"></i>
                            搜索
                        </button>

                    </form>

                    <div class="alert alert-success fade show">
                        <span class="close" data-dismiss="alert">×</span>
                        <strong>注释事项!</strong><br/>
                        1、文章管理<br/>
                        2、发布系统文章<br/>
                    </div>

                    <div class="btn-group" style="margin-bottom: 10px;">
                        <button type="button" class="btn btn-sm btn-primary m-r-5" onclick="add()">
                            <i class="fa fa-plus-circle"></i>
                            新增
                        </button>
                        <button type="button" class="btn btn-sm btn-danger m-r-5" id="deleteAll">
                            <i class="fa fa-archive"></i>
                            删除
                        </button>
                    </div>

                    <!-- begin table-responsive -->
                    <div class="table-responsive">
                        <table class="table table-bordered m-b-0">
                            <thead>
                            <tr>
                                <th>
                                    <div class="checkbox checkbox-css">
                                        <input type="checkbox" value="" id="ids" />
                                        <label for="ids">&nbsp;</label>
                                    </div>
                                </th>
                                <th>入库时间</th>
                                <th>文章作者</th>
                                <th>文章标题</th>
                                <th>操作</th>
                            </tr>
                            </thead>
                            <tbody>
                            <c:forEach items="${iPage.data}" var="obj" varStatus="objStat">
                                <tr>
                                    <td class="with-checkbox">
                                        <div class="checkbox checkbox-css">
                                            <input type="checkbox" id="obj_${obj.id}" name="obj" value="${obj.id}" />
                                            <label for="obj_${obj.id}">&nbsp;</label>
                                        </div>
                                    </td>
                                    <td >${localDateTimeUtil.format(obj.addTime,'yyyy-MM-dd HH:mm:ss')}</td>
                                    <td >${obj.author}</td>
                                    <td >${obj.title}</td>
                                    <td class="with-btn" nowrap>
                                        <a href="javascript:void(0);" class="btn btn-sm btn-primary width-60 m-r-2" onclick="editById(${obj.id})">
                                            <i class="fa fa-edit"></i> Edit
                                        </a>
                                        <a href="javascript:void(0);" class="btn btn-sm btn-danger width-80" onclick="deleteById(${obj.id})">
                                            <i class="fa fa-archive"></i> Delete
                                        </a>
                                        <a href="javascript:void(0);" class="btn btn-sm btn-danger width-80" onclick="window.location.href='${basePath}article/show?id=${obj.id}'">
                                            <i class="fa fa-archive"></i> Show
                                        </a>
                                    </td>
                                </tr>
                            </c:forEach>
                            </tbody>
                        </table>
                    </div>
                    <!-- end table-responsive -->

                    <%@include file="../../common/common-pages.jsp"%>

                </div>
                <!-- end panel-body -->
            </div>
            <!-- end panel -->

        </div>
        <!-- end col-6 -->
    </div>
    <!-- end row -->
</div>
<!-- end #content -->
<!-- end page container -->

<%@include file="../../common/common-script.jsp"%>

<script>
    var ctx = '${basePath}'

    function add(){
        window.location.href=ctx+'website/article/add'
    }

    function deleteById(id){
        var ids = [id];
        delByIds(ids)
    }

    function editById(id){
        window.location.href=ctx+'website/article/add?id='+id
    }

    function delByIds(ids){
        $.ajax({
            type:'post',
            url:ctx+'website/article/delete',
            data:JSON.stringify(ids),
            success:function(res){
                if(res == "1000"){
                    window.location.href=ctx+'website/article/list'
                }else{
                    swal({
                        title: '删除异常！',
                        text: '该信息不可删除!',
                        icon: 'error',
                        buttons: {
                            cancel: {
                                text: 'Cancel',
                                value: null,
                                visible: true,
                                className: 'btn btn-default',
                                closeModal: true,
                            },
                            confirm: {
                                text: 'Warning',
                                value: true,
                                visible: true,
                                className: 'btn btn-danger',
                                closeModal: true
                            }
                        }
                    });
                }
            }
        })
    }

    $(function(){
        $('#ids').click(function(){
            $("input[name='obj']:checkbox").prop("checked",this.checked);
        })

        $("#deleteAll").click(function(){
            var ids = [];
            $(":checkbox[name='obj']:checked").each(function() {
                ids.push($(this).val())
            });

            delByIds(ids)

        })

        $("#search").click(function(){
            var url = ctx+'website/article/list?current=1'

            var author = $("#author").val()
            var title = $("#title").val()

            if(author != null && author != ''){
                url += '&author='+author
            }

            if(title != null && title != ''){
                url += '&title='+title
            }
            window.location.href=url
        })

    })

    function goTo(current){
        var url = ctx+'website/article/list?current=?'+current
        var author = $("#author").val()
        var title = $("#title").val()

        if(author != null && author != ''){
            url += '&author='+author
        }

        if(title != null && title != ''){
            title += '&title='+title
        }
        window.location.href=url

    }
</script>

</body>
</html>
